package com.quan.weixin.api.pay;

import java.text.ParseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.quan.common.bean.R;
import com.quan.common.core.MyBaseController;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/api/wx/mp")
public class WxMpPayController extends MyBaseController {

	@Autowired
	private WxPayService wxPayService;

	/**
	 * 预支付交易会话标识
	 * 
	 * @param unifiedOrderRequest
	 * @return
	 */
	@PostMapping(value = "pay")
	public Object pay(@RequestBody WxPayUnifiedOrderRequest unifiedOrderRequest) {
		log.info("==>1、微信支付，调用统一下单接口，获取”预支付交易会话标识“");

		try {
			// 必填字段 body, outTradeNo, totalFee, spbillCreateIp, notifyUrl, tradeType
//			unifiedOrderRequest.setSpbillCreateIp(IpHelper.getIpAddr(request));
//			unifiedOrderRequest.setNotifyUrl(this.systemValueProperties.getWechat().getPayNotifyUrl());
//			unifiedOrderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);
//
//			Object createOrder = this.wxPayService.createOrder(unifiedOrderRequest);
//
//			return ResultBean.ok(createOrder);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.failure("微信支付，调用统一下单接口，获取”预支付交易会话标识“失败");
	}

	/**
	 * 支付回调通知处理
	 * 
	 * @param xmlData
	 * @return
	 * @throws WxPayException
	 * @throws ParseException 
	 */
	@PostMapping("/notify/order")
	public String parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException, ParseException {
		log.info("==>2、支付回调通知处理");
		final WxPayOrderNotifyResult notifyResult = this.wxPayService.parseOrderNotifyResult(xmlData);
		
		// 支付完成时间
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//		Date date = simpleDateFormat.parse(notifyResult.getTimeEnd());
//		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		LocalDateTime payTime = DateUtils.UDateToLocalDateTime(simpleDateFormat1.parse(simpleDateFormat1.format(date)));

		
		log.info("==>支付结果：{}", notifyResult);
		log.info("==>微信支付订单号.：{}", notifyResult.getTransactionId());
		log.info("==>订单金额：{}", notifyResult.getTotalFee());
		log.info("==>商户订单号：{}", notifyResult.getOutTradeNo());
		log.info("==>支付完成时间：{}", notifyResult.getTimeEnd());
		log.info("==>交易类型：{}", notifyResult.getTradeType());
		log.info("----------------------------支付完成----------------------------");
		

		return WxPayNotifyResponse.success("成功");
	}

}
